package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by chensi on 2018/3/9.
 */

public class X3AccountDialog extends Dialog {
    private Point mScreenPoint = new Point();
    private Context mRootActivity = null;

    private TextView mTitleView;
    private TextView mDescriptionView;
    private Button mRightBtnView;
    private Button mLeftBtnView;
    private View v_cancel;
    private ImageView iv_icon;
    private View v_line;
    private TextView tv_preview;

    private String mTitle;
    private String mDescription;
    private String mCancelText;
    private String mOkText;
    private boolean isForce = false;
    private boolean isFlag = false;
    private String previewStr;

    /**
     * 没有按钮的提示dialog
     */
    public X3AccountDialog(Context arg, String des, boolean isFlag) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        mDescription = des;
        this.isFlag = isFlag;
    }

    /**
     * 一个按钮Dialog
     *
     * @param arg
     * @param title
     * @param des
     */
    public X3AccountDialog(Context arg, String title, String des) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        mTitle = title;
        previewStr = des;
    }

    public TextView getTv_preview() {
        return tv_preview;
    }

    /**
     * 一个按钮Dialog
     *
     * @param arg
     * @param title
     * @param des
     */
    public X3AccountDialog(Context arg, String title, String des, boolean isForce) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        mTitle = title;
        mDescription = des;
        this.isForce = isForce;
    }

    /**
     * 两个按钮的Dialog
     *
     * @param arg
     * @param title
     * @param des
     */
    public X3AccountDialog(Context arg, String title, String des,
                           String cancelText, String okText) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        mTitle = title;
        mCancelText = cancelText;
        mOkText = okText;
        mDescription = des;
    }

    public X3AccountDialog(Context arg, String des,
                           String cancelText, String okText) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        mCancelText = cancelText;
        mOkText = okText;
        mDescription = des;
    }

    /**
     * 两个按钮的Dialog
     *
     * @param arg
     * @param title
     * @param des
     */
    public X3AccountDialog(Context arg, String title, String des, String preview,
                           String cancelText, String okText) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        previewStr = preview;
        mTitle = title;
        mCancelText = cancelText;
        mOkText = okText;
        mDescription = des;
    }

    public void setTitle(String text) {
        mTitle = text;
        if (mTitleView != null) {
            mTitleView.setText(mTitle);
        }
    }

    public void setDescription(String Text) {
        mDescription = Text;
        if (mDescriptionView != null) {
            mDescriptionView.setText(mDescription);
        }
    }

    public void setLeftBtnText(String cancelText) {
        mCancelText = cancelText;
        if (mLeftBtnView != null) {
            mLeftBtnView.setText(cancelText);
        }
    }

    public void setRightBtnText(String okText) {
        mOkText = okText;
        if (mRightBtnView != null) {
            mRightBtnView.setText(okText);
        }
    }

    public void setLeftClick(View.OnClickListener cancelClick) {
        if (mLeftBtnView != null) {
            mLeftBtnView.setOnClickListener(cancelClick);
        }
    }

    public void setRightClick(View.OnClickListener okClick) {
        if (mRightBtnView != null) {
            mRightBtnView.setOnClickListener(okClick);
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_point_dialog);
        initDialogWindow();
        initView();
        setOnKeyListener((dialog, keyCode, event) -> isForce);
    }

    void initView() {
        mRightBtnView = findViewById(R.id.btn_affirm);
        mLeftBtnView = findViewById(R.id.btn_cancel);
        mTitleView = findViewById(R.id.tv_title);
        mDescriptionView = findViewById(R.id.tv_content);
        iv_icon = findViewById(R.id.iv_icon);
        v_line = findViewById(R.id.v_line);
        v_cancel = findViewById(R.id.v_cancel);
        tv_preview = findViewById(R.id.tv_preview);

        if (isFlag) {
            v_line.setVisibility(View.GONE);
            mLeftBtnView.setVisibility(View.GONE);
            mRightBtnView.setVisibility(View.GONE);
            iv_icon.setVisibility(View.VISIBLE);
        } else {
            mLeftBtnView.setVisibility(View.VISIBLE);
            mRightBtnView.setVisibility(View.VISIBLE);
            iv_icon.setVisibility(View.GONE);
            v_line.setVisibility(View.VISIBLE);
        }

        tv_preview.setText(previewStr + "");
        tv_preview.setVisibility(X3StringUtils.isEmpty(previewStr) ? View.GONE : View.VISIBLE);

        mTitleView.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mTitle)) {
            mTitleView.setText(mTitle);
        } else {
            mTitleView.setVisibility(View.GONE);
        }

        mDescriptionView.setVisibility(X3StringUtils.isEmpty(mDescription) ? View.GONE : View.VISIBLE);
        mDescriptionView.setText(mDescription);
        if (!TextUtils.isEmpty(mOkText)) {
            mRightBtnView.setText(mOkText);
        }
        if (!TextUtils.isEmpty(mCancelText)) {
            mLeftBtnView.setText(mCancelText);
            mLeftBtnView.setOnClickListener(v -> {
                dismiss();
                if (iBtnClick != null) {
                    iBtnClick.iBtnCancelClick();
                }
            });
        } else {
            mLeftBtnView.setVisibility(View.GONE);
            v_cancel.setVisibility(View.GONE);
        }
        mRightBtnView.setOnClickListener(v -> {
            dismiss();
            if (iBtnClick != null) {
                iBtnClick.iBtnAffirmClick();
            }
        });
    }

    IBtnClick iBtnClick;
    public void setiBtnClick(IBtnClick iBtnClick) {
        this.iBtnClick = iBtnClick;
    }
    public interface IBtnClick {
        void iBtnAffirmClick();

        void iBtnCancelClick();
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = dialogWindow.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.85);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("提示信息弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("提示信息弹窗");
    }
}

